#include "../import/head.h"
#include "helper_heart.h"
#include "../import/import.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "../helper_net/head.h"
#include "../helper_tcp/head.h"
#include "../helper_udp/head.h"

////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_sgsj
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    helper_heart::helper_heart()
    {
    }

    helper_heart::~helper_heart()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // proc

    int helper_heart::proc()
    {
        int ret = 0;

		session::LIST & s_list = g_pSysData->m_pPool->m_session_list;

		if( ( 0 == ret ) && ( g_pSysData->m_bQuiting || g_pSysData->m_bQuit ) )
		{
			ret = 1;
		}

		for( session * p = s_list.begin(); ( 0 == ret ) && ( p != s_list.end() ); p = p->m_pNext )
		{
			if( session::TYPE_TCP_LISTEN == p->m_eType )
			{
				continue;
			}

			if( p->m_bTimeout || p->m_bQuiting )
			{
				continue;
			}

			timeout_check( p );
			emit_heart( p );
		}

		if( 0 == ret )
		{
			show_info();
		}

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

	int helper_heart::timeout_check( session * pSession )
	{
		int ret = 0;

		pSession->net_timeout_s++;

		if( pSession->net_timeout_s >= g_pSysData->m_config.net_timeout_s )
		{
			LOG4( "(%s,%d) timeout second = %d", pSession->m_pTcp->m_szIp, pSession->m_pTcp->m_nPort, pSession->net_timeout_s );
			pSession->m_bTimeout = true;
			pSession->sub_ref( 1 );
		}
		else
		{
			LOG4( "(%s,%d) timeout check = %d", pSession->m_pTcp->m_szIp, pSession->m_pTcp->m_nPort, pSession->net_timeout_s );
		}

		return ret;
	}

    int helper_heart::emit_heart( session * pSession )
    {
        int ret = 0;

		protocol_heart obj;
        obj.compose( pSession->m_pTcp );

        return ret;
    }

    int helper_heart::show_info()
    {
        int ret = 0;

        if( 0 == ret )
        {
            g_pSysData->m_nCurIntervalS++;
            if( g_pSysData->m_nCurIntervalS >= g_pSysData->m_config.show_info_interval_s )
            {
                g_pSysData->m_nCurIntervalS = 0;
            }
            else
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            LOG4( "----------------------------------begin show info-----------------------------------------------" );
            LOG4( "msg num = %d", g_pSysData->m_os.m_pXos->get_total_pool_size( xos::i_xos::XOS_OBJ_MSG, 0 ) );
            LOG4( "buf num = %d", g_pSysData->m_os.m_pXos->get_total_pool_size( xos::i_xos::XOS_OBJ_BUF, 0 ) );
            LOG4( "data num = %d", g_pSysData->m_pPool->m_data_pool.GetTotalCountNoLock() );
            LOG4( "udp num = %d", g_pSysData->m_pPool->m_udp_pool.GetTotalCountNoLock() );
            LOG4( "tcp num = %d", g_pSysData->m_pPool->m_tcp_pool.GetTotalCountNoLock() );
            LOG4( "----------------------------------end show info-----------------------------------------------" );
        }

        return ret;
    }

} // xos_sgsj
